package com.nus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nus.parsers.ParserAPIs;
import com.nus.representations.IvleModule;
import com.nus.representations.IvleNews;
import com.nus.representations.IvleRosters;
import com.nus.representations.IvleWorkbin;

public class IvleRosterActivity extends Activity {
	private ListView lv;

	private String lv_arr[];
	protected TextView title;

	private ArrayList<IvleRosters> rosters;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.ivlerosterlayout);
		 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
		 title = (TextView) findViewById(R.id.titleheader);
		 title.setText("Class Roster");
		 ImageView b = (ImageView)findViewById(R.id.home);
		  b.setImageDrawable(getResources().getDrawable(R.drawable.ivle));
		String url = "https://ivle.nus.edu.sg/api/Lapi.svc/Class_Roster?APIKey=W7RtgW7GZKUpaEkJ1NwUD&AuthToken="
				+ NUS_Android.IVLE_Token
				+ "&CourseID="
				+ IvleModuleActivity.COURSE_ID + "&output=json";
		try {
			String response = getResponseText(url);

			Log.i("Response", response);

			rosters = ParserAPIs.parseIvleRosters(response);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		lv = (ListView) findViewById(R.id.list_view);
		lv.setAdapter(new ListAdapter(IvleRosterActivity.this, R.id.rosterLV,
				rosters));

	}

	public String getResponseText(String stringUrl) throws IOException {
		StringBuilder response = new StringBuilder();

		URL url = new URL(stringUrl);
		HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
		if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					httpconn.getInputStream()), 8192);
			String strLine = null;
			while ((strLine = input.readLine()) != null) {
				response.append(strLine);
			}
			input.close();
		}
		return response.toString();
	}

	private class ListAdapter extends ArrayAdapter<IvleRosters> { // --
		// CloneChangeRequired
		private ArrayList<IvleRosters> mList; // --CloneChangeRequired
		private Context mContext;

		public ListAdapter(Context context, int textViewResourceId,
				ArrayList<IvleRosters> list) { // --CloneChangeRequired
			super(context, textViewResourceId, list);
			this.mList = list;
			this.mContext = context;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			try {
				if (view == null) {
					LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					view = vi.inflate(R.layout.ivlerosterlistitem, null);
				}
				final IvleRosters listItem = mList.get(position);
				if (listItem != null) {
					// setting list_item views
					TextView tv = (TextView) view
							.findViewById(R.id.student_name);
					tv.setText(listItem.getUsername());
					TextView tv2 = (TextView) view
							.findViewById(R.id.student_email);
					tv2.setText(listItem.getEmail());
				}
			} catch (Exception e) {
				Log.i(IvleRosterActivity.ListAdapter.class.toString(),
						e.getMessage());
			}
			return view;
		}
	}

}
